package com.qiwo.common.controller;


import com.qiwo.common.base.AdminBaseController;
import com.qiwo.common.domain.ContentDO;
import com.qiwo.common.service.ContentService;
import com.qiwo.common.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 
 * <pre>
 * 单篇管理
 *  @author  chenwenchao
 *  @date 2018-10-07 17:14:04
 * </pre>
 * <small> 2018-10-07 17:14:04 | chenwenchao</small>
 */
@Controller
@RequestMapping("/common/content")
public class ContentController extends AdminBaseController {
	@Autowired
	private ContentService contentService;
	
	@GetMapping()
	@RequiresPermissions("common:content:content")
	String Content(){
	    return "common/content/content";
	}


	@GetMapping("/edit/{typeid}")
	@RequiresPermissions("common:content:edit")
	String edit(@PathVariable("typeid") Long typeId, Model model){
		ContentDO content = contentService.findOneByKv("type_id",typeId);
		if(content==null){
			content=new ContentDO();
			content.setTypeId(typeId);
		}
		model.addAttribute("content", content);
	    return "common/content/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public Result<String> save( ContentDO content){
		if(content.getTypeId()==14){
			content.setTitle(content.getContent());
		}
		contentService.insert(content);
        return Result.ok();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("common:content:edit")
	public Result<String>  update( ContentDO content){
		if(content.getId()!=null) {
			boolean update = contentService.updateById(content);
			return update ? Result.ok() : Result.fail();
		}else{
			contentService.insert(content);
			return Result.ok();
		}
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("common:content:remove")
	public Result<String>  remove( Integer id){
		contentService.deleteById(id);
        return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("common:content:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] ids){
		contentService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
